﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using Chowpoo.Util;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{
    public class SupportController : BaseController
    {
        //
        // GET: /Support/

        public ActionResult Index(int? type)
        {
            type = type.CheckInt(0);
            ViewData["type"] = type;
            if (type == 0)
            {
                var info = CourseSettingBLL.Get();
                ViewData["Content"] = info.CourseGuide;
                return View();
            }
            if (type == 1)
            {
                return RedirectToAction("Helper");
            }
            if (type == 2)
                return RedirectToAction("Process");
            if (type == 3)
                return RedirectToAction("Note");
            return NotFound();
        }

        public ActionResult Helper()
        {
            ViewData["type"] = 1;
            var info = CourseSettingBLL.Get();
            ViewData["Content"] = info.CourseHelper;
            return View();
        }


        public ActionResult Note()
        {
            ViewData["type"] = 3;
            return View();
        }

        public ActionResult NoteListUc(int? pid)
        {
            CommonRequest.ClearCache();
            var list = StudentNoteBLL.GetList().OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        [UserAuth(IsLimt = true, UserType = UserTypeEnum.Student)]
        public ActionResult NoteEditUc(int? id)
        {
            if (id.HasValue)
            {
                var info = StudentNoteBLL.Get(id.Value);
                if (info == null)
                    return NotFound();
                return PartialView(info);
            }
            return PartialView(new StudentNoteInfo { });
        }

        [HttpPost]
        [UserAuth(IsLimt = true, UserType = UserTypeEnum.Student)]
        public ActionResult NoteEditUc(StudentNoteInfo info)
        {
            ViewData.Model = info;
            if (!ModelState.IsValid)
                return PartialView();
            if (info.Id != 0)
            {
                var oldInfo = StudentNoteBLL.Get(info.Id);
                if (oldInfo == null)
                    return NotFound();
                oldInfo.NoteContent = info.NoteContent;
                if (StudentNoteBLL.Edit(oldInfo))
                {
                    SetHighLineMsg("编辑成功.");
                    return PartialView(oldInfo);
                }

                SetHighLineMsg("未知错误.");
                return PartialView();

            }
            info.NoteTime = DateTime.Now;
            info.StuId = UserHelper.LoginUser.Id;
            if (StudentNoteBLL.Add(info))
            {
                SetHighLineMsg("编辑成功.");
                return PartialView();
            }

            SetHighLineMsg("未知错误.");
            return PartialView();
        }

        [UserAuth(IsLimt = true, UserType = UserTypeEnum.Student)]
        public ActionResult NoteDel(int id)
        {
            if (StudentNoteBLL.Del(id))
            {
                SetAlertMsg("删除成功.",true);
            }
            return RedirectToAction("Note");
        }
    }
}
